﻿using UnityEngine;
using System.Collections.Generic;
namespace Room
{
    public class RoomManager
    {
        #region static
        private static Dictionary<SceneType, RoomBase> sceneLogics = new Dictionary<SceneType, RoomBase>();

        public static RoomBase GetRoom(SceneType sceneType)
        {
            if (sceneLogics.ContainsKey(sceneType))
            {
                return sceneLogics[sceneType];
            }
            else
            {
                if (sceneType == SceneType.LOGIN /*|| sceneType == SceneType.BATTLE*/)
                {
                    return null;
                }
                GameObject go = new GameObject(sceneType.ToString());
                var slm = AddRoomLogic(go, sceneType);
                slm.Init();
                slm.transform.parent = ManagerController.Instance.transform;
                sceneLogics.Add(sceneType, slm);
                return slm;
            }
        }

        private static RoomBase AddRoomLogic(GameObject go, SceneType st)
        {
            switch (st)
            {
                case SceneType.LOGIN:
                    return null;
                case SceneType.BATTLE:
                    return go.AddComponent<RoomBattle>();
                case SceneType.TOWN:
                    return go.AddComponent<RoomTown>();
                case SceneType.COPY:
                    return go.AddComponent<RoomCopy>();
                case SceneType.FIELD:
                    return go.AddComponent<RoomField>();
                case SceneType.RAID:
                    return go.AddComponent<RoomRaid>();
                case SceneType.RUSH:
                    return go.AddComponent<RoomRush>();
                case SceneType.CARD_VIEW:
                    return go.AddComponent<RoomCardView>();
                case SceneType.UNION:
                    return go.AddComponent<RoomUnion>();
                case SceneType.UNIONFIGHT:
                    return go.AddComponent<RoomUnionFight>();
                default:
                    return null;
            }
        }

        public static RoomBase currentRoom;
        public static SceneType curSceneType;

        public static void Switch(SceneType sceneType, params System.Object[] param)
        {
            //Debug.Log("SwitchScene from " + curSceneType + " to " + nextSceneType);

            if (currentRoom != null)
            {
                currentRoom.Leave();
                currentRoom.gameObject.SetActive(false);
            }

            curSceneType = sceneType;

            var cs = GetRoom(curSceneType);
            currentRoom = cs;
            if (cs != null)
            {
                cs.gameObject.SetActive(true);
                cs.Enter(param);
            }

            Room.RoomModelPool.Instance.SwitchRoom(curSceneType);
        }

        #endregion static
    }
}